﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;

namespace gChartSharp
{
    public class LinearGradientFillParam : IFill
    {

        /// <summary>
        /// The name of the API
        /// </summary>
        public string Name { get { return "chf"; } }


        /// <summary>
        /// Always return what type of fill we are in case we forget
        /// </summary>
        public FillType FillType { get { return FillType.lg; } }


        /// <summary>
        /// bg for background fill or c for chart area fill
        /// </summary>
        public LinearGradientFillType LinearGradientType { get; set; }


        /// <summary>
        /// Specifies the angle of the gradient between 0 (horizontal) and 90 (vertical)
        /// </summary>
        public double GradientAngle
        {
            get { return mGradientAngle; }
            set
            {
                if (value < 0 || value > 90)
                    throw new ParamException("GradientAngle value must be between 0 and 90.");

                mGradientAngle = value;
            }
        }
        private double mGradientAngle;


        /// <summary>
        /// The first color of the gradient fill
        /// </summary>
        public Color Color1 { get; set; }


        /// <summary>
        /// The second color of the gradient fill
        /// </summary>
        public Color Color2 { get; set; }


        /// <summary>
        /// Specify at what point the color is pure where: 0 specifies the right-most chart position and 1 the left-most.
        /// </summary>
        public double Offset1
        {
            get { return mOffset1; }
            set
            {
                if (value < 0 || value > 1)
                    throw new ParamException("Offset value must be between 0 and 1.");

                mOffset1 = value;
            }
        }
        private double mOffset1;


        /// <summary>
        /// Specify at what point the color is pure where: 0 specifies the right-most chart position and 1 the left-most.
        /// </summary>
        public double Offset2
        {
            get { return mOffset2; }
            set
            {
                if (value < 0 || value > 1)
                    throw new ParamException("Offset value must be between 0 and 1.");

                mOffset2 = value;
            }
        }
        private double mOffset2;


        /// <summary>
        /// Instantiates all properties
        /// </summary>
        /// <param name="linearGradientType"></param>
        /// <param name="gradientAngle"></param>
        /// <param name="color"></param>
        /// <param name="offset"></param>
        public LinearGradientFillParam(LinearGradientFillType linearGradientType, double gradientAngle, Color color1, double offset1, Color color2, double offset2)
        {
            LinearGradientType = linearGradientType;
            GradientAngle = gradientAngle;
            Color1 = color1;
            Offset1 = offset1;
            Color2 = color2;
            Offset2 = offset2;
        }


        /// <summary>
        /// The default constructor
        /// </summary>
        public LinearGradientFillParam() { }


        /// <summary>
        /// Render the API text
        /// </summary>
        /// <returns></returns>
        public string Render()
        {
            StringBuilder sb = new StringBuilder();

            if (Color1 != default(Color) || Color2 != default(Color) 
                || GradientAngle != default(double) || LinearGradientType != default(LinearGradientFillType)
                || Offset1 != default(double) || Offset2 != default(double))
            {
                sb.AppendFormat("&{0}={1},{2},{3},{4},{5},{6}", Name, LinearGradientType.ToString(),
                    FillType.ToString(), Color1.ToHtml(), Offset1.ToString(),
                    Color2.ToHtml(), Offset2.ToString());
            }

            return sb.ToString();
        }
    }


    public enum LinearGradientFillType
    {
        [DescriptionAttribute("bg")]
        Background,

        [DescriptionAttribute("c")]
        ChartArea
    }
}
